package Views;

import Controllers.SearchController;
import Views.TableModels.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;

public class SearchPanel extends javax.swing.JPanel
{
	private SearchController controller = new SearchController();
	private SearchableTabbedPane tabbedPane;
	private ImageIcon[] images;
	private String[] titles;

	public SearchPanel(SearchableTabbedPane parent)
	{
		this.tabbedPane = parent;
		this.titles = TabIndex.getTitles();
		this.images = TabIndex.getImages();
		initComponents();
		
		Integer[] intArray = new Integer[titles.length];
		for (int i = 0; i < titles.length; i++) {
			intArray[i] = new Integer(i);
		}

		sectionCB.setModel(new DefaultComboBoxModel(intArray));
		ComboBoxRenderer renderer = new ComboBoxRenderer(titles, images);
		sectionCB.setRenderer(renderer);

		resultTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				if (e.getClickCount() == 2)
				{
					if(resultTable.getSelectedRowCount() == 1)
					{
						chooseBActionPerformed(null);
					}
				}
			}
		});
	}

	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        sectionL = new javax.swing.JLabel();
        sectionCB = new javax.swing.JComboBox();
        searchL = new javax.swing.JLabel();
        searchTF = new javax.swing.JTextField();
        searchB = new javax.swing.JButton();
        showAllB = new javax.swing.JButton();
        searchTableSP = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();
        chooseB = new javax.swing.JButton();

        setMaximumSize(new java.awt.Dimension(600, 420));
        setMinimumSize(new java.awt.Dimension(600, 420));
        setName("Form"); // NOI18N

        sectionL.setText("Emne:");
        sectionL.setName("sectionL"); // NOI18N

        sectionCB.setName("sectionCB"); // NOI18N

        searchL.setText("Søg:");
        searchL.setName("searchL"); // NOI18N

        searchTF.setName("searchTF"); // NOI18N

        searchB.setText("Søg");
        searchB.setName("searchB"); // NOI18N
        searchB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBActionPerformed(evt);
            }
        });

        showAllB.setText("Vis alle");
        showAllB.setName("showAllB"); // NOI18N
        showAllB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllBActionPerformed(evt);
            }
        });

        searchTableSP.setName("searchTableSP"); // NOI18N

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        resultTable.setName("resultTable"); // NOI18N
        searchTableSP.setViewportView(resultTable);

        chooseB.setText("Vælg");
        chooseB.setName("chooseB"); // NOI18N
        chooseB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseBActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(searchTableSP, javax.swing.GroupLayout.DEFAULT_SIZE, 580, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(sectionL)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(sectionCB, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(searchL)
                        .addGap(18, 18, 18)
                        .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(searchB)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(showAllB))
                    .addComponent(chooseB, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sectionL)
                    .addComponent(sectionCB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchL)
                    .addComponent(searchB)
                    .addComponent(searchTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(showAllB))
                .addGap(18, 18, 18)
                .addComponent(searchTableSP, javax.swing.GroupLayout.DEFAULT_SIZE, 297, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chooseB)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void searchBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_searchBActionPerformed
	{//GEN-HEADEREND:event_searchBActionPerformed
		String searchText = searchTF.getText();
		int selectedSection = sectionCB.getSelectedIndex();
		
		if(selectedSection == TabIndex.Customers.getIndex())
		{
			resultTable.setModel(new CustomerTableModel(controller.searchCustomer(searchText)));
		}
		else if(selectedSection == TabIndex.Locations.getIndex())
		{
			resultTable.setModel(new LocationTableModel(controller.searchLocation(searchText)));
		}
		else if(selectedSection == TabIndex.Routes.getIndex())
		{
			resultTable.setModel(new RouteTableModel(controller.searchRoute(searchText)));
		}
		else if(selectedSection == TabIndex.Rounds.getIndex())
		{
			resultTable.setModel(new RoundTableModel(controller.searchRound(searchText)));
		}
		else if(selectedSection == TabIndex.Employees.getIndex())
		{
			resultTable.setModel(new EmployeeTableModel(controller.searchEmployee(searchText)));
		}
		else if(selectedSection == TabIndex.Cars.getIndex())
		{
			resultTable.setModel(new CarTableModel(controller.searchCar(searchText)));
		}
		else if(selectedSection == TabIndex.Schedules.getIndex())
		{
			resultTable.setModel(new ScheduleTableModel(controller.searchSchedule(searchText)));
		}
	}//GEN-LAST:event_searchBActionPerformed

	private void showAllBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_showAllBActionPerformed
	{//GEN-HEADEREND:event_showAllBActionPerformed
		searchTF.setText("*");
		searchBActionPerformed(null);
	}//GEN-LAST:event_showAllBActionPerformed

	private void chooseBActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_chooseBActionPerformed
	{//GEN-HEADEREND:event_chooseBActionPerformed
		int selectedSection = sectionCB.getSelectedIndex();
		int chosenId = Integer.parseInt(resultTable.getValueAt(resultTable.getSelectedRow(), 0).toString());
		tabbedPane.switchTabWithNo(TabIndex.getTabIndexFromIndex(selectedSection), chosenId);
	}//GEN-LAST:event_chooseBActionPerformed

	public void startSearch(int section, String searchText)
	{
		sectionCB.setSelectedIndex(section);
		searchTF.setText(searchText);
		searchBActionPerformed(null);
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton chooseB;
    private javax.swing.JTable resultTable;
    private javax.swing.JButton searchB;
    private javax.swing.JLabel searchL;
    private javax.swing.JTextField searchTF;
    private javax.swing.JScrollPane searchTableSP;
    private javax.swing.JComboBox sectionCB;
    private javax.swing.JLabel sectionL;
    private javax.swing.JButton showAllB;
    // End of variables declaration//GEN-END:variables
}